package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.domain.ProjectPlan;
import com.abvnet.project.water.project.domain.ProjectPlanDescription;
import com.abvnet.project.water.project.domain.ProjectStatus;
import com.abvnet.project.water.project.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目方案信息操作处理
 * 
 * @author duj
 * @date 2019-07-30
 */
@Controller
@RequestMapping("/project/projectPlan")
public class ProjectPlanController extends BaseController {
    private String prefix = "project/projectPlan";
	
	@Autowired
	private IProjectPlanService projectPlanService;

    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IProjectMicrobialService projectMicrobialService;

    @Autowired
    private IProjectCaseService projectCaseService;

    @Autowired
	private IProjectPlanDescriptionService projectPlanDescriptionServer;

	@RequiresPermissions("project:projectPlan:list")
	@GetMapping()
	public String list() {
	    return prefix + "/list";
	}
	
	/**
	 * 查询项目方案列表
	 */
	@RequiresPermissions("project:projectPlan:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectInfo projectInfo) {
		if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
			projectInfo.setCreateBy(ShiroUtils.getLoginName());
			projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
		}
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
	}

    @RequiresPermissions("project:projectPlan:list")
    @GetMapping("/plan/{projectId}")
    public String projectPlan(@PathVariable("projectId") Long projectId, ModelMap mmap) {
	    mmap.put("projectId", projectId);
        return prefix + "/plan";
    }

    @RequiresPermissions("project:projectPlan:list")
    @GetMapping("/view/{projectId}")
    public String view(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectId", projectId);
        return prefix + "/view";
    }

    @RequiresPermissions("project:projectPlan:list")
    @PostMapping("/plan")
    @ResponseBody
    public TableDataInfo plan(ProjectPlan projectPlan) {
        startPage();
        List<ProjectPlan> list = projectPlanService.selectProjectPlanList(projectPlan);
        return getDataTable(list);
    }
	
	/**
	 * 新增项目方案
	 */
	@GetMapping("/add/{projectId}")
	public String add(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectId", projectId);
        mmap.put("mList", projectMicrobialService.selectMicrobialList());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目方案
	 */
	@RequiresPermissions("project:projectPlan:add")
	@Log(title = "项目方案", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectPlan projectPlan) {
        ProjectInfo projectInfo = projectInfoService.selectProgressCodeById(projectPlan.getProjectId());
//        if (StringUtils.equals(ProjectStatus.TEST.getCode(),projectInfo.getProgressCode())) {
//            projectInfo.setProgressCode(ProjectStatus.SCHEME.getCode());
//            projectInfoService.updateProjectInfo(projectInfo);
//        }
		if (Integer.parseInt(projectInfo.getProgressCode()) < Integer.parseInt(ProjectStatus.SCHEME.getCode())) {
			projectInfo.setProgressCode(ProjectStatus.SCHEME.getCode());
			projectInfoService.updateProjectInfo(projectInfo);
		}
		return toAjax(projectPlanService.insertProjectPlan(projectPlan));
	}

	/**
	 * 修改项目方案
	 */
	@GetMapping("/edit/{planId}")
	public String edit(@PathVariable("planId") Long planId, ModelMap mmap) {
		ProjectPlan projectPlan = projectPlanService.selectProjectPlanById(planId);
		mmap.put("projectPlan", projectPlan);
        mmap.put("mList", projectMicrobialService.selectMicrobialList());
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目方案
	 */
	@RequiresPermissions("project:projectPlan:edit")
	@Log(title = "项目方案", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectPlan projectPlan) {
		return toAjax(projectPlanService.updateProjectPlan(projectPlan));
	}

    @RequiresPermissions("project:projectPlan:edit")
    @GetMapping("/detail/{projectId}")
    public String detail(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectCase", projectCaseService.selectProjectCaseByProjectId(projectId));
        return prefix + "/detail";
    }

	/**
	 * 删除项目方案
	 */
	@RequiresPermissions("project:projectPlan:remove")
	@Log(title = "项目方案", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(projectPlanService.deleteProjectPlanByIds(ids));
	}

	/**
	 * 新增项目方案描述
	 */
	@GetMapping("/addDescription/{projectId}")
	public String addDescription(@PathVariable("projectId") Long projectId, ModelMap mmap) {
		ProjectPlanDescription projectPlanDescription = projectPlanDescriptionServer.selectProjectPlanDescriptionByProjectId(projectId);
		mmap.put("projectId", projectId);
		if (projectPlanDescription != null) { // 编辑
			mmap.put("projectPlanDescription", projectPlanDescription);
			return prefix + "/editDescription";
		}
		return prefix + "/addDescription";
	}

	/**
	 * 新增保存项目方案描述
	 */
	@RequiresPermissions("project:projectPlan:add")
	@Log(title = "项目方案", businessType = BusinessType.INSERT)
	@PostMapping("/addDescription")
	@ResponseBody
	public AjaxResult addDescription(ProjectPlanDescription projectPlanDescription) {
		return toAjax(projectPlanDescriptionServer.insertProjectPlanDescription(projectPlanDescription));
	}

	/**
	 * 新增保存项目方案描述
	 */
	@RequiresPermissions("project:projectPlan:edit")
	@Log(title = "项目方案", businessType = BusinessType.UPDATE)
	@PostMapping("/editDescription")
	@ResponseBody
	public AjaxResult editDescription(ProjectPlanDescription projectPlanDescription) {
		return toAjax(projectPlanDescriptionServer.updateProjectPlanDescription(projectPlanDescription));
	}
}
